package cn.lopr.im.android.mvp.view.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import java.util.List;

import cn.lopr.im.android.R;
import cn.lopr.im.android.bean.ArticleCommentVo;
import cn.lopr.im.android.bean.ArticleVo;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.mvp.contact.IArticleViewContact;
import cn.lopr.im.android.mvp.presenter.ArticleViewPresenterImpl;
import cn.lopr.im.android.mvp.view.activities.CommentDetailActivity;
import cn.lopr.im.android.mvp.view.activities.InfoActivity;
import cn.lopr.im.android.mvp.view.activities.MainActivity;
import cn.lopr.im.android.mvp.view.adapters.CommunityArticleCommentAdapter;


public class BtnBottomDialog extends BottomSheetDialogFragment implements IArticleViewContact.IArticleViewView {

    List<ArticleCommentVo> articleCommentVos;
    private RecyclerView rlComment;
    private CommunityArticleCommentAdapter communityArticleCommentAdapter;
    private String TAG;
    private String articleId;
    private BottomSheetDialog dialog;
    private String replyCommentId;
    private IArticleViewContact.IArticleViewPresenter articleViewPresenter;
    private User replyCommentUser;
    private Context context;
    private TextView etContent;
    private TextView tvSend;
    private OnBottomDialogClickListener mOnClickListener;


    public BtnBottomDialog(Context context, String articleId) {
        this.context = context;
        this.articleId = articleId;
        articleViewPresenter = new ArticleViewPresenterImpl(this);
    }

    @SuppressLint({"Range", "SetTextI18n"})
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (getActivity() == null) return super.onCreateDialog(savedInstanceState);

        dialog = new BottomSheetDialog(getActivity(), R.style.BottomSheet);

        View root = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_comment_list, null);
        dialog.setContentView(root);

        initWidget();
        initData();
        //设置宽度
        ViewGroup.LayoutParams params = root.getLayoutParams();
        params.height = (int) (0.80 * getResources().getDisplayMetrics().heightPixels);

        root.setLayoutParams(params);
        Window window = dialog.getWindow();
        if (window != null) {
            window.setWindowAnimations(R.style.BottomSheet);
            //WindowManager.LayoutParams params2 = window.getAttributes();
            //params2.dimAmount = 0.0f;
            //window.setAttributes(params2);
        }
        return dialog;
    }

    @SuppressLint("SetTextI18n")
    private void initWidget() {
        BottomSheetBehavior<FrameLayout> dialogBehavior = dialog.getBehavior();
        dialogBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        rlComment = dialog.findViewById(R.id.rl_comment);
        ImageView ivClose = dialog.findViewById(R.id.iv_close);
        etContent = dialog.findViewById(R.id.et_content);
        tvSend = dialog.findViewById(R.id.tv_send);
        ivClose.setOnClickListener(e -> {
            dialog.dismiss();
            if (this.mOnClickListener != null) {
                this.mOnClickListener.onClick(dialogBehavior, 0);
            }
        });
        tvSend.setOnClickListener(e -> this.articleViewPresenter.addComment(this.getToken(this.getContext())));
    }

    void initData() {
        if (!TextUtils.isEmpty(articleId)) {
            this.articleViewPresenter.getComment(this.getToken(getContext()), 1, 20);
        }

    }

    @Override
    public void show(@NonNull FragmentManager manager, @Nullable String tag) {
        //在show之前设置弹出动画
        super.show(manager, tag);
        //在show之后设置关闭动画
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void getCommentSuccess(List<ArticleCommentVo> articleCommentVos) {

        if (articleCommentVos == null || articleCommentVos.size() == 0) {
            dialog.findViewById(R.id.tv_empty).setVisibility(View.VISIBLE);
            dialog.findViewById(R.id.rl_comment).setVisibility(View.GONE);
            return;
        }

        this.communityArticleCommentAdapter = new CommunityArticleCommentAdapter(dialog.getContext(), articleCommentVos);
        this.rlComment.setAdapter(this.communityArticleCommentAdapter);
        this.communityArticleCommentAdapter.onRecyclerViewItemClickListener(new CommunityArticleCommentAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int section) {
                Log.i(TAG, "onItemClick: " + view.getId());
                ArticleCommentVo articleCommentVo = (ArticleCommentVo) view.getTag();
                if (view.getId() == R.id.ll_publish_comment_user) {
                    Intent intent = new Intent(context.getApplicationContext(), InfoActivity.class);
                    intent.putExtra("user", articleCommentVo.getCurrentUser());
                    startActivity(intent);
                } else if (view.getId() == R.id.iv_reply_comment) {
                    Log.i(TAG, "onItemClick: iv_reply_comment " + view.getId());
                    replyCommentUser = articleCommentVo.getCurrentUser();
                    replyCommentId = articleCommentVo.getId();
                    openInput();
                } else if (view.getId() == R.id.tv_reply_all) {
                    Intent intent = new Intent(context.getApplicationContext(), CommentDetailActivity.class);
                    intent.putExtra("commentId", articleCommentVo.getId());
                    startActivity(intent);
                }
            }

            @Override
            public void onItemLongClick(View view, int section) {

            }
        });

        dialog.findViewById(R.id.tv_empty).setVisibility(View.GONE);
        dialog.findViewById(R.id.rl_comment).setVisibility(View.VISIBLE);
        TextView tvCountComment = dialog.findViewById(R.id.tv_count_comment);
        tvCountComment.setText(articleCommentVos.size() + "条评论");
        rlComment = dialog.findViewById(R.id.rl_comment);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void addCommentSuccess() {
        closeInput();
        etContent.setText("");
        articleViewPresenter.getComment(this.getToken(this.getContext()), 1, 20);
    }

    @Override
    public void getArticleSuccess(ArticleVo articleVo) {

    }

    @Override
    public String getComment() {
        return etContent.getText().toString();
    }

    @Override
    public String getArticleId() {
        return articleId;
    }

    @Override
    public Integer getUserId() {
        return MainActivity.user.getId();
    }

    @Override
    public String getReplyCommentId() {
        return replyCommentId;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void closeInput() {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(etContent.getWindowToken(), 0);
        etContent.setHint("   写评论...");
    }

    public void openInput() {
        etContent.setFocusable(true);
        etContent.setFocusableInTouchMode(true);
        //请求获得焦点
        etContent.requestFocus();
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(etContent, 0);

        if (!TextUtils.isEmpty(replyCommentId)) {
            etContent.setHint("   回复" + this.replyCommentUser.getNickname());
        }
    }

    public void onClickListener(OnBottomDialogClickListener mOnClickListener) {
        this.mOnClickListener = mOnClickListener;
    }

    public interface OnBottomDialogClickListener {
        void onClick(BottomSheetBehavior<FrameLayout> view, int section);

        void onLongClick(View view, int section);
    }
}

